class: center, middle, inverse, title-slide .title[ # A Bayesian method for generating distribution-based trading signals in pairs trading ] .subtitle[ ##
How to make money in financial markets using stats
] .author[ ### Allan Quadros ] .date[ ### Ph.D. Candidate | Statistics Kansas State University 2024-11-06 ] --- layout: true background-image: url(./img/logo/logo2.png) background-position: 0% 100% background-size: 5% <style type="text/css"> .highlight-last-item > ul > li, .highlight-last-item > ol > li { opacity: 0.5; } .highlight-last-item > ul > li:last-of-type, .highlight-last-item > ol > li:last-of-type { opacity: 1; } </style> --- ## Table of contents <!-- <br> --> <!-- > Objectives --> <br> <br> > (1) Introduction <br><br> > (2) Proposed Method <br><br> > (3) Results <br><br> > (4) Conclusion <br><br> > (5) Future Research <br><br> --- class: inverse, center, middle <!-- title-slide-section-grey, --> ## Introduction --- </br> </br> ###<font color =#4C5455><code>What is (statistical) arbitrage?</code></font> <!-- ??? Pairs trading is a type of statistical arbitrage. So we need to rewinf a little bit and understand what is statistical arbitrage. and to understand what is statistical arbitrage, we need to first understand what is arbitrage --> <br> > __Arbitrage__ <br><br> Take advantage of price differences in different markets for the same or different assets <!-- ??? comecar pelo exemplo --> <br> <br> > __Statistical arbitrage__ <br><br> When we use stats to do arbitrage <!-- ??? we can use stats to identify assets, markets and the best time to trade with stats --> --- class: highlight-last-item ### <code>__Pairs trading:__ <font color =#4C5455>what is it?</font></code> </br> </br> </br> -- + Pairs trading is a investment strategy that exploits temporary mispricings between two assets that historically __move together__ </br> -- + When the pair deviates from its historical norm, investors __`BUY`__ (take a __long__ position in) the undervalued asset and __`SELL`__ (take a __short__ position in) the overvalued one, expecting a reversion of this spread to its historical average or levels (Avellaneda and Lee, 2010) </br> -- + Its main appeal lies in producing a __low-volatility__ and __market-neutral__ investment strategy (Gatev _et al._, 2006) <!-- pode ser bonds, contracts, options, stocks etc --> </br> -- + It was first employed by a quantitative group at Morgan Stanley in the 1980s <!-- </br> --> <!-- -- --> <!-- + It belongs to a broader class of investment strategy called statistical arbitrage - statistical modeling of price relationships among different assets to generate excess returns --> --- ###<font color =#4C5455><code>Example of co-moving assets: __Coca-Cola vs. Pepsi__</code></font>
--- ###<font color =#4C5455><code>__Pairs trading__: how to make money from it?</code></font> <img src="index_files/figure-html/unnamed-chunk-3-1.png" width="720" style="display: block; margin: auto;" /> --- class: highlight-last-item ###<font color =#4C5455><code>__Pairs trading__: what are the main challenges?</code></font> <br> <br> > 1. __Identifying pairs__ of securities that exhibit a __stable relationship__ in the desired time frame <br><br> > 2. Optimal __share allocation__ between the two assets to appropriately hedge against market volatility <br><br> > 3. Generating __accurate trading signals__ to precisely time the entry and exit points. <br><br> --- ###<font color =#4C5455><code>Cointegration strategy: __pair selection__</code></font> <code> Cointegration test - a two step procedure designed by Engle & Granger (1987): </code> <br> <br> `$$\Delta y_t = \alpha + \theta t + \gamma y_{t-1} + \delta_1 \Delta y_{t-1} + \delta_2 \Delta y_{t-2} + \cdots + \delta_p \Delta y_{t-p} + \epsilon_t$$` <!-- Nesta equação: --> <!-- Δyt - representa a variação de yt em relação ao período anterior. --> <!-- α é o termo constante. Eh onde a serie comeca --> <!-- theta t representa uma tendência temporal determinística (se hah trend ou nao) --> <!-- \gamma é o termo que testa a presença de raiz unitária. --> <!-- ∑δΔy t−i - são os termos de defasagem das primeiras diferenças, incluídos para capturar a autocorrelação serial. --> <!-- ϵt - é o termo de erro (estocastico - de fora do modelo) --> <!-- A inclusão de Δyt e de suas defasagens permite testar a hipótese nula de que a série possui uma raiz unitária (não estacionária) contra a alternativa de que é estacionária. Essa abordagem é fundamental para determinar se os resíduos de uma regressão de cointegração são estacionários, indicando a existência de uma relação de equilíbrio de longo prazo entre as séries analisadas. --> -- > __One:__ Select two stocks, say `\(X\)` and `\(Y\)`, that historically __move together__ and test (using Augmented-Dickey Fuller - ADF) if the two price series ( `\(y_t\)` and `\(x_t\)` ) are __non-stationary__, i.e. both series have an unit root `\(\gamma = 0\)` <br><br> -- > __Two:__ Fit a linear model `\(\hat{y_t} = \hat{\beta_0} + \hat{\beta_1} x_t\)`, and test (using ADF) the __residuals__ ( `\(e_t\)` ) for stationarity, i.e., `\(e_t\)` does not have an unit root ( `\(\gamma < 0\)` ) <!-- ??? where `\(I(1)\)` denotes an integrated process of order 1, meaning that the series becomes stationary only after taking the first difference. --> <br> -- + If __`(1)`__ and __`(2)`__ hold, then the series are said to be __cointegrated__ - stocks `\(X\)` and `\(Y\)` share a __long-term equilibrium relationship__ --- ###<font color =#4C5455><code>Cointegration strategy: __trading__</code></font> > Trading signals are generated based on the standard deviations of the spread `\(y_t - \hat{\beta_1} x_t\)` .pull-left[ > __Positions__ are taken whenever the standardized spread crosses the `\(\pm k\sigma\)` thresholds, going long (or short) `\(\hat{\beta_1}\)` dollars of stock `\(X\)` for each dollar of stock `\(Y\)`. > __Take-profit__ signals are emitted when the Z-score reverts back to `0` - the long-term average > __Stop-loss__ triggers are activated when the Z-score reaches a defined loss margin `\(\xi\)` from the entry point ( `\(\pm|k\sigma + \xi|\)` ) ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-4-1.png" width="504" style="display: block; margin: auto;" /> ] <!-- ??? LEGAL: O alpha nao precisa entrar no calculo justamente pelo motivo do qual eu distorci o primeiro grafico com log(PEP) - c. Sem o c, o efeito seria o mesmo, ou sejam nao interessa a distancia absoluta entre KO e PEP - o que interesse eh o desvio relativo entre ambas e nao o desvio total considerando o nivel inicial. Alem do fato de que ao icnluirmos alpha, teremos mais uma variavel para estimar tornando o modelo mais complexo e prone to more errors and overfitting--> <!-- ??? falar do zscore e mostrar o grafico 2 (mudar as cores - usar o codigo do palomar) - falar sobre transformar as duas series em um ativo sintetico --> --- class: inverse, center, middle <!-- title-slide-section-grey, --> ## Proposed Method --- ###<font color =#4C5455><code> Proposed method: __motivation__ </code></font> <code> Problems with the standard cointegration method: </code></br></br> -- > High false positive rates when used within a data mining approach</br></br> -- > Has been widely adopted by both institutional and individual investors</br></br> -- > Can result in premature/delayed exits <!-- ??? alpha level is inflated --> <!-- ??? less opportunities - the margins have shrunk --> <!-- ??? practically speaking, relying only on the zscore of the spread is not very effective -- sometimes the relationship between the securities changes and the zscore does not capture that. Soetimes, it is too sensitive. --> <!-- ??? Competes against high frequency trading to identify distortions ? --> --- ###<font color =#4C5455><code> Proposed method: __hypotheses__ </code></font> > __Idea:__ `\(\hat{\beta_1}\)` carries important information about the linear relationship between the underlying __co-moving__ securities</br></br> -- > __Proposal:__ Add a __confirmation layer__ to the standard cointegration strategy in pairs trading by evaluating the behavior of the __hedge ratio__ `\(\hat{\beta_1}\)`</br></br> -- > __How:__ Deriving the __full-conditional distribution__ of `\(\beta_1\)` from OLS regression through a __hierarchical Bayesian model__, and establishing two thresholds within this distribution that will serve as control/confirmation boundaries for the trading strategy. -- .pull-left[ <img src="index_files/figure-html/unnamed-chunk-5-1.png" width="432" style="display: block; margin: auto;" /> ] -- .pull-right[ <code> __Hypotheses:__ </code> > __(i)__ produces more accurate trading signals > __(ii)__ increases false discovery rates (FDR) in cointegration test ] --- ###<font color =#4C5455><code> Theoretical Background: __hierarchical Bayesian model__ </code></font> <code> Using results from Gelfand _et al._ (1992), Hooten & Hefley (2019), and Rencher (2007), we have: </code> -- <code> __Likelihood:__ </code> .my-style2[ > `$$y_i \sim \mathcal{N}(\boldsymbol{X\beta}, \sigma^2)$$` ] .my-style[ where `\(\boldsymbol{\beta} = \begin{bmatrix} \beta_0 & \beta_1 \end{bmatrix}^\top\)` ] -- <code> __Priors:__ </code> .my-style2[ > `$$\sigma^2 \sim \mathcal{IG}(q, r)$$` > `$$\boldsymbol{\beta} \sim \mathcal{TN}_{\beta_1>0}^{\infty}(\boldsymbol{\mu_{\beta}} = \mathbf{(X'X)^{-1}X'y}, \boldsymbol{\Sigma_{\beta}} = \sigma_0^2 \mathbf{(X'X)^{-1}})$$` ] .my-style[ where `\(\widehat{\sigma_0^2} = \frac{1}{n-2}\boldsymbol{y'(I - H)y}\)` and `\(\boldsymbol{H = X(X'X)^{-1}X'}\)` ] -- <code> __Conjugate full-conditional posteriors:__ </code> .my-style2[ > `$$\boldsymbol{\beta} \mid \boldsymbol{y}, \sigma^2 \equiv \mathcal{TN}_{\beta_1 > 0}^{\infty}(\boldsymbol{\mu} = \boldsymbol{A^{-1}b}, \boldsymbol{\Sigma} = \boldsymbol{A^{-1}})$$` ] .my-style[ where `\(\boldsymbol{A} \equiv \boldsymbol{X}^\top (\sigma^2\boldsymbol{I})^{-1} \boldsymbol{X} + \boldsymbol{\Sigma_\beta}^{-1}\)` and `\(\boldsymbol{b} \equiv \boldsymbol{X}^\top (\sigma^2\boldsymbol{I})^{-1} \boldsymbol{y} + \boldsymbol{\Sigma_\beta}^{-1} \boldsymbol{\mu_\beta}\)` ] .my-style2[ > `$$\sigma^2 \mid \boldsymbol{y, \beta} \equiv \mathcal{IG}(\tilde q, \tilde r)$$` ] .my-style[ where `\(\tilde q = q + \frac{n}{2}\)` and `\(\tilde r = \left[\frac{1}{2}\boldsymbol{(y - X\beta)'(y - X\beta)} + \frac{1}{r}\right]^{-1}\)` ] --- class: inverse, center, middle <!-- title-slide-section-grey, --> ## Results --- ###<font color =#4C5455><code> Empirical Results: __data & methodology__ </code></font> <br> <br> -- > Yahoo finance data, daily timeframe <br><br> -- > More than 10 years of data: __`01-01-2014 - 05-10-2024`__ <br><br> -- > Backtest consisted of a sliding window approach (forming | decision | trading periods) <br><br> -- > The algorithm was implemented in R --- ###<font color =#4C5455><code> Empirical Results: __backtesting__ </code></font> <iframe src="animated_z_score_with_spike_and_stabilization6(3).html" width="900" height="500" style="border:none;"></iframe> <!-- ```{r} --> <!-- # Carrega o arquivo HTML contendo a animação Plotly --> <!-- htmltools::includeHTML("animated_z_score_with_spike_and_stabilization6.html") --> <!-- ``` --> --- ###<font color =#4C5455><code> Empirical Results: __U.S.__ </code></font> <br> <br> <br> .center[ [__LINK__](./results/US2.html) <!-- <iframe src="./results/US2.html" width="800" height="600" style="border:none;"></iframe> --> ] --- ###<font color =#4C5455><code> Empirical Results: __Japan__ </code></font> <br> <br> <br> .center[ [__LINK__](./results/Japan2.html) <!-- <iframe src="./results/US2.html" width="800" height="600" style="border:none;"></iframe> --> ] --- ###<font color =#4C5455><code> Empirical Results: __Brazil__ </code></font> <br> <br> <br> .center[ [__LINK__](./results/Brazil2.html) ] --- ###<font color =#4C5455><code> Empirical Results: __Commodities__ </code></font> <br> <br> <br> .center[ [__LINK__](./results/Commodity2.html) ] --- class: inverse, center, middle <!-- title-slide-section-grey, --> ## Conclusions --- class: highlight-last-item ## <font color =#4C5455><code> Conclusions </code></font> <br> -- > __Improved profitability and risk management__ <br><br> -- > __Dynamic and precise trading signals__ <br><br> -- > __Improved False Discovery Rates for the cointegration test__ <!-- The Bayesian algorithm enhanced both profitability and risk management compared to the traditional cointegration strategy, especially in ETFs. --> <!-- The method’s ability to adjust entry and exit signals dynamically based on hedge ratio distribution led to more timely and accurate signals. --> <!-- The Bayesian approach demonstrated a reduction in false positives in cointegration tests, proving useful for pair selection and potentially applicable beyond quantitative finance. --> --- class: inverse, center, middle <!-- title-slide-section-grey, --> ## Future Research --- ## <code> __Future Research__ </code> <!-- por exemplo outros assets: cryptocurrencies --> > Apply the method to __other markets and assets__, test __different trading parameters__, and __include transaction costs__ <br> > Development of a __follow-up test for cointegration__ in specific scenarios <br> > Similar strategy based on __Moving Block Bootstrap__ <br> > Test a __Deep Reinforcement Learning__ approach in pairs trading --- ## <font color =#4C5455><code> References </code></font> Alvin C. Rencher, G.B.S., Linear Models in Statistics, 2nd , 2007 (John Wiley & Sons, Inc: USA). Avellaneda, M. and Lee, J.H., Statistical arbitrage in the US equities market. _Quantitative Finance_, 2010, 10, 761–782. Engle, R.F. and Granger, C.W.J., Co-Integration and Error Correction: Representation, Estimation, and Testing. _Econometrica_, 1987, 55, 251–276. Gatev, E., Goetzmann, W.N. and Rouwenhorst, K.G., Pairs trading: Performance of a relative-value arbitrage rule. _The Review of Financial Studies_, 2006, 19, 797–827. Gelfand, A.E., Smith, A.F.M. and Lee, T.M., Bayesian Analysis of Constrained Parameter and Truncated Data Problems Using Gibbs Sampling. _Journal of the American Statistical Association_, 1992, 87, 523–532. Hooten, M. and Hefley, T., Bringing Bayesian Models to Life, 1st , 2019 (CRC Press: USA). --- class: inverse, center, middle <!-- title-slide-section-grey, --> ## Thank you! ### quadros@ksu.edu